/*
* 1videoConference -- An open source video conferencing platform.
*
* Copyright (C) 2007 - 2008, Adiance Technologies Pvt. Ltd.
*
* Hardik Sanghvi <hardik.sanghvi @adiance.com>
*
* See http://www.1videoconference.org for more information about
* the 1videoConference project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, forums and mailing lists
* for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Drawing;

using System.Runtime.InteropServices;
using _videoConference.BAL;


namespace _videoConference
{
    #region for Converter class declaration

    public class CIntToStringConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (double.Parse(value.ToString()) - 24.0 >= 0.0)
            {
                return double.Parse(value.ToString()) - 24.0;
            }
            else
            {
                return 0.0;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
        }
    }
    #endregion

    public partial class ctlMyPOD : System.Windows.Controls.UserControl,IDisposable
    {
        public double DiffX = 0;
        public double DiffY = 0;
        public double OldLeft = 0.0;
        public double OldTop = 0.0;

        double oldWidth = 0;
        double oldHeight = 0;
        public double TitleX = 0;
        public double TitleY = 0;
        public bool mDown = false;

        public bool mHeight = false;
        public bool mWidth = false;

        bool maxPOD = false;
        string ctlname = "";

        double x, y;
        bool maxPODID = false;
        int podId = 0;
        int modId = 0;
        
        private bool disposed = false;

        IAddPage ia = new IAddPage();

        public ctlMyPOD()
        {
            InitializeComponent();
            oldHeight = this.Height;
            oldWidth = this.Width;

            this.Tag = "";

            if (UserInfo.MyRole == "Host")
            {
                btnMaximized.Visibility = Visibility.Visible;
                btnMinimized.Visibility = Visibility.Visible;
            }
            else
            {
                btnMaximized.Visibility = Visibility.Collapsed;
                btnMinimized.Visibility = Visibility.Collapsed;
            }

            cnvBody.SizeChanged += new SizeChangedEventHandler(cnvBody_SizeChanged);            
        }

        void cnvBody_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            for (int i = 0; i < cnvBody.Children.Count; i++)
            {
                if (cnvBody.Children[i].GetType().ToString() == cnvBody.Tag.ToString())
                {
                    if (e.NewSize.Height >= 3.0)
                    {
                        ((UserControl)cnvBody.Children[i]).Height = e.NewSize.Height - 3.0;                        
                        ((UserControl)cnvBody.Children[i]).Width = e.NewSize.Width - 3.0;
                    }
                    else
                    {
                        ((UserControl)cnvBody.Children[i]).Height = e.NewSize.Height;
                        ((UserControl)cnvBody.Children[i]).Width = e.NewSize.Width;
                    }
                }
            }
        }

        public string Text
        {
            get { return lblTitle.Content.ToString(); }
            set { lblTitle.Content = value; }
        }

        public string ControlName
        {
            get { return ctlname; }
            set { ctlname = value; }
        }

        public int PID
        {
            get { return podId; }
            set { podId = value; }
        }

        public int ModID
        {
            get { return modId; }
            set { modId = value; }
        }

        public bool maxid
        {
            get { return maxPODID; }
            set { maxPODID = value; }
        }

        void ctlMyPOD_Unloaded(object sender, RoutedEventArgs e)
        { 
           
        }

        void ctlMyPOD_Loaded(object sender, RoutedEventArgs e)
        {           
            OldLeft = (double)this.GetValue(InkCanvas.LeftProperty);
            OldTop = (double)this.GetValue(InkCanvas.TopProperty);
            oldHeight = this.Height;
            oldWidth = this.Width;
        }

        public void ctlMyPOD_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
           this.Tag = this.Parent;
           ((InkCanvas)this.Tag).Children.Remove((ctlMyPOD)sender);
           ((InkCanvas)this.Tag).Children.Add((ctlMyPOD)sender);
                    
        }

        void cnvPOD_PreviewMouseUp(object sender, MouseButtonEventArgs e)
        {
           
                if (e.GetPosition(this).X >= this.Width - 5 && e.GetPosition(this).Y >= this.Height - 5)
                {
                    mHeight = false;
                    mWidth = false;
                    ((InkCanvas)(this.Parent)).Tag = "Mouse Up";
                }
                else if (e.GetPosition(this).X <= this.Width - 5 && e.GetPosition(this).Y >= this.Height - 5)
                {
                    mHeight = false;
                    ((InkCanvas)(this.Parent)).Tag = "Mouse Up";
                }
                else if (e.GetPosition(this).X >= this.Width - 5 && e.GetPosition(this).Y <= this.Height - 5)
                {
                    mWidth = false;
                    ((InkCanvas)(this.Parent)).Tag = "Mouse Up";
                }
           
        }

        void cnvPOD_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
           
                if (e.GetPosition(this).X >= this.Width - 5 && e.GetPosition(this).Y >= this.Height - 5)
                {
                    mHeight = true;
                    mWidth = true;
                    ((InkCanvas)(this.Parent)).Tag = this;
                }
                else if (e.GetPosition(this).X <= this.Width - 5 && e.GetPosition(this).Y >= this.Height - 5)
                {
                    mHeight = true;
                    ((InkCanvas)(this.Parent)).Tag = this;
                }
                else if (e.GetPosition(this).X >= this.Width - 5 && e.GetPosition(this).Y <= this.Height - 5)
                {
                    mWidth = true;
                    ((InkCanvas)(this.Parent)).Tag = this;
                }
                  
          
         }

        void cnvPOD_PreviewMouseMove(object sender, MouseEventArgs e)
         {
             if (UserInfo.MyRole == "Host")
             {
                 if (e.GetPosition(this).X >= cnvPOD.Width - 5 && e.GetPosition(this).Y >= cnvPOD.Height - 5)
                 {
                     this.Cursor = Cursors.SizeNWSE;
                 }
                 else if (e.GetPosition(this).X <= cnvPOD.Width - 5 && e.GetPosition(this).Y >= cnvPOD.Height - 5)
                 {
                     this.Cursor = Cursors.SizeNS;
                 }
                 else if (e.GetPosition(this).X >= cnvPOD.Width - 5 && e.GetPosition(this).Y <= cnvPOD.Height - 5)
                 {
                     this.Cursor = Cursors.SizeWE;
                 }
                 else if (this.Cursor != Cursors.SizeAll)
                 {
                     this.Cursor = Cursors.Arrow;
                 }
             }
         }

        void cnvTitle_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (UserInfo.MyRole == "Host")
            {
                ((InkCanvas)(this.Parent)).Tag = this;
                mDown = true;
                DiffX = e.GetPosition((InkCanvas)this.Parent).X - this.Margin.Left;
                DiffY = e.GetPosition((InkCanvas)this.Parent).Y - this.Margin.Top;
                TitleX = e.GetPosition(this).X;
                TitleY = e.GetPosition(this).Y;
                this.Cursor = Cursors.SizeAll;
            }          
        }

        void cnvTitle_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if (UserInfo.MyRole == "Host")
            {
                ((InkCanvas)(this.Parent)).Tag = "Mouse Up";
                mDown = false;
                OldLeft = (double)this.GetValue(InkCanvas.LeftProperty);
                OldTop = (double)this.GetValue(InkCanvas.TopProperty);
                this.Cursor = Cursors.Arrow;
            }
        }

        public void btnMaximized_Click(object sender, RoutedEventArgs e)
        {
            if (UserInfo.MyRole == "Host")
            {
                if (maxPOD)
                {
                    this.SetValue(InkCanvas.LeftProperty, OldLeft);
                    this.SetValue(InkCanvas.TopProperty, OldTop);
                    this.Height = oldHeight;
                    this.Width = oldWidth;
                    btnMaximized.ToolTip = "Maximize";
                    maxPOD = false;
                }
                else
                {
                    oldHeight = cnvPOD.Height;
                    oldWidth = cnvPOD.Width;
                    OldLeft = (double)this.GetValue(InkCanvas.LeftProperty);
                    OldTop = (double)this.GetValue(InkCanvas.TopProperty);

                    this.Height = 450;
                    this.Width = 600;
                    this.SetValue(InkCanvas.LeftProperty, 5.0);
                    this.SetValue(InkCanvas.TopProperty, 5.0);

                    btnMaximized.ToolTip = "Restore";
                    maxPOD = true;
                }
            }

            else
            {
                if (maxPOD)
                {
                }
                else
                {
                    oldHeight = cnvPOD.Height;
                    oldWidth = cnvPOD.Width;
                    OldLeft = (double)this.GetValue(InkCanvas.LeftProperty);
                    OldTop = (double)this.GetValue(InkCanvas.TopProperty);
                }
            }

        }

        public void btnMinimized_Click(object sender, RoutedEventArgs e)
        {            
            //this.Visibility = Visibility.Collapsed;
        }
                
        public void PodVisible(bool flag)
        {
            if (flag)
            {
                this.Visibility = Visibility.Visible;
            }
            else
            {
                this.Visibility = Visibility.Collapsed;
            }

            Type[] t3 = cnvBody.Children[1].GetType().GetInterfaces();


            if (UserInfo.strMeetingMode == "Meeting Mode")
            {
                if (flag)
                {

                    for (int i = 0; i < t3.Length; i++)
                    {
                        if (t3[i].Name == "IsvcPodVisibleChange")
                        {
                            try
                            {
                                object[] obj = new object[] { true };
                                System.Reflection.MethodInfo mi = t3[i].GetMethod("PodVisibility");
                                mi.Invoke(cnvBody.Children[1], obj);
                                break;
                            }
                            catch (Exception exp)
                            {
                                exp.Data.Add("My Key", "1videoConference--:--ctlPOD.xaml.cs--:--PodVisible--:----:--");
                                clsUtility.LogException = exp;
                            }
                        }
                    }
                }
                else
                {

                    for (int i = 0; i < t3.Length; i++)
                    {
                        if (t3[i].Name == "IsvcPodVisibleChange")
                        {
                            try
                            {
                                object[] obj = new object[] { false };
                                System.Reflection.MethodInfo mi = t3[i].GetMethod("PodVisibility");
                                mi.Invoke(cnvBody.Children[1], obj);
                                break;
                            }
                            catch (Exception exp)
                            {
                                exp.Data.Add("My Key", "1videoConference--:--ctlPOD.xaml.cs--:--PodVisible()--:----:--");
                                clsUtility.LogException = exp;
                            }
                        }
                    }
                }
            }
        }    
     
         public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);    
        }

         private void Dispose(bool disposing)
        {
            if(!this.disposed)
            {
                if(disposing)
                { 
                   
                }        
                disposed = true;

            }
        }

        ~ctlMyPOD()
        {
            Dispose(false);
        }



    }
}